package com.sirap.spring.boot.demo.controller.album;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XCodeUtil;
import com.sirap.fish.fetchers.CatImageFetchers;
import com.sirap.fish.fetchers.CatXImageFetchers;
import com.sirap.fish.fetchers.CatYImageFetchers;

@Controller
@RequestMapping(value = "/albumcatx")
public class AlbumCatXController extends TopAlbumController {

	private static final String WORDS_TOPK_PATH = "may/cats.topk.txt";
	
	@Override
	@RequestMapping(value = "/list")
	public String listAlbums(HttpServletRequest request, HashMap<String, Object> map) {
		D.at();
		
		return StrUtil.occupy("album/album{0}List", type());
	}

	@Override
	@ResponseBody
    @RequestMapping(value = "/page/{base64url}")
	public Map<String, Object> getAlbumsByPageUrl(@PathVariable String base64url, HttpServletRequest request) {
		D.at();
    	String niceurl = XCodeUtil.fromBase64(base64url);
    	D.pla(base64url, niceurl);
    	
    	String location = StrUtil.useSeparator(storage(), WORDS_TOPK_PATH);
    	List<String> lines = IOUtil.readLinesWithUTF8(location);
		
		D.pl(location);
		
		Map<String, Object> map = CatImageFetchers.getAlbumsByPersonUrl(niceurl, lines);
		
		D.pjsp(map);
	
		return map;
	}

	@Override
	@RequestMapping(value = "/show/{base64url}")
	public String showAlbum(HttpServletRequest request, @PathVariable String base64url, HashMap<String, Object> map) {
		D.at();
		String niceurl = XCodeUtil.fromBase64(base64url);
    	D.pla(base64url, niceurl);
    	
    	if(useX(niceurl)) {
    		D.pl("xiezhen");
    		map.putAll(getAlbumDetail(base64url, CatXImageFetchers.class));
    	} else {
    		D.pl("mneinv");
    		map.putAll(getAlbumDetail(base64url, CatYImageFetchers.class));
    	}
    	
		return StrUtil.occupy("album/album{0}Detail", "Cat");
	}
	
	private boolean useX(String niceurl) {
		return StrUtil.contains(niceurl, "xiezhen");
	}

	@Override
	@ResponseBody
	@RequestMapping(value = "/count/{base64url}")
	public Map<String, Object> countOfImages(HttpServletRequest request, @PathVariable String base64url) {
		D.at();
		
    	String niceurl = XCodeUtil.fromBase64(base64url);
    	D.pla(base64url, niceurl);
    	
    	String count = "";
    	
    	if(useX(niceurl)) {
    		D.pl("xiezhen");
    		count = CatXImageFetchers.getAlbum(niceurl).getLinks().size() + "";
    	} else {
    		D.pl("mneinv");
    		count = CatYImageFetchers.countOfImages(niceurl);
    	}
    	
		return Amaps.createMap("count", count);
	}
}
